package com.firesys.rounting.Adapter;

import java.util.List;

import com.firesys.R;
import com.firesys.povo.DeviceItem;
import com.firesys.povo.DeviceSubItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * 选择巡检类型适配器
 * @author King
 *2013-4-23
 */
public class DeviceRoutingCheckAdapter extends BaseAdapter implements OnItemSelectedListener {
    
    //Spiner 选项内容
    private List<DeviceItem> item;
    
    //巡检条目说明
    private List<Integer>  info;
    
    private Context context;
    
    private LayoutInflater inflater;
    
    

    public DeviceRoutingCheckAdapter(List<DeviceItem> item,List<Integer> info,Context context){
	this.info=info;
	this.context=context;
	this.item=item;
	this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
	
	return item.size();
    }

    @Override
    public Object getItem(int position) {
	
	return item.get(position);
    }

    @Override
    public long getItemId(int position) {
	// TODO Auto-generated method stub
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	ViewHolder holder=null;
	DeviceItem  device=item.get(position);
	if(convertView==null){
	    convertView = inflater.inflate(R.layout.device_routing_check_item, parent, false);
	    holder=new ViewHolder();
	    holder.textview=(TextView)convertView.findViewById(R.id.device_routing_check_item_info);
	    holder.spinner=(Spinner)convertView.findViewById(R.id.device_routing_check_item_spinner);
	    
	    holder.spinner.setOnItemSelectedListener(this);
	    convertView.setTag(holder);
	}else{
	    holder=(ViewHolder)convertView.getTag();
	}
	
	holder.textview.setText(device.getDeviceItemName());
	
//	if(info.get(position)==0&&!isSelectInfo(device.getSubitem())){
//	    List<DeviceSubItem> l=device.getSubitem();
//	    DeviceSubItem d=new DeviceSubItem();
//	    d.setNumber("0");
//	    d.setName("请选择");
//	    l.add(0,d);
//	    ArrayAdapter<DeviceSubItem>    adapter=new ArrayAdapter<DeviceSubItem>(context, android.R.layout.simple_spinner_item, l);
//	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
//	        holder.spinner.setAdapter(adapter);
//	}else{
//	    ArrayAdapter<DeviceSubItem>    adapter=new ArrayAdapter<DeviceSubItem>(context, android.R.layout.simple_spinner_item, device.getSubitem());
//	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
//	        holder.spinner.setAdapter(adapter);
//	}
	
	   ArrayAdapter<DeviceSubItem>    adapter=new ArrayAdapter<DeviceSubItem>(context, android.R.layout.simple_spinner_item, device.getSubitem());
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
	        holder.spinner.setAdapter(adapter);
        holder.spinner.setSelection(info.get(position), true);
        holder.spinner.setTag(position+"");
     
	return convertView;
    }

    
    //加载性能优化
  public   class ViewHolder{
	
	public TextView textview=null;
	
	public Spinner spinner=null;

	public TextView getTextview() {
	    return textview;
	}

	public void setTextview(TextView textview) {
	    this.textview = textview;
	}

	public Spinner getSpinner() {
	    return spinner;
	}

	public void setSpinner(Spinner spinner) {
	    this.spinner = spinner;
	}
	
	
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
	
	
	try{
	    DeviceSubItem  sub=(DeviceSubItem)parent.getSelectedItem();        
	    	 
	   info.set(selectItem(sub.getDeviceItemNumbeer()), position);
	}catch(Exception e){
	    e.printStackTrace();
	}
        
      
       
	
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
	
	
    }
    public List<DeviceItem> getItem() {
        return item;
    }
    public void setItem(List<DeviceItem> item) {
        this.item = item;
    }
    public List<Integer> getInfo() {
        return info;
    }
    public void setInfo(List<Integer> info) {
        this.info = info;
    }

  public boolean isSelectInfo(List<DeviceSubItem> list){
      for(int i=0;i<list.size();i++){
	  if(list.get(i).getName().equals("请选择")){
	      return true;
	  }
      }
    return false;
  }

  public List<DeviceSubItem> delectSelectInfo(List<DeviceSubItem> list){
      for(int i=0;i<list.size();i++){
	  if(list.get(i).getName().equals("请选择")){
	  list.remove(i);
	  return list;
	  }
      }
    return list;
  }
  
  /**
   * 得到被选中的项目
   * @param itemnumber
   * @return
   */
  public int selectItem(String itemnumber){
      int number=0;
      for(int i=0;i<item.size();i++){
	  if(item.get(i).getDeviceItemNumber().equals(itemnumber)){
	      return i;
	  }
      }
    return number;
  }
}
